﻿using System;
using System.Runtime.InteropServices;

namespace BMproductions.Media.MediaPlayer.WMPPlus {
	internal enum WMPPlusProperty: int {
		HookMenu,
		HookWMPClose,
		WMPVersion,
		MediaFolder,
		MediaFolderList,
		LibMenu,
		AddonsTagEditor,
		AddonsContextMenu,
		AddonsTagEditorLabel,
		AddonsWMPTitle,
		AddonsTitle,
		SettingsMSNEnable,
		SettingsMSNHideOnPause,
		SettingsMSNShowVideo,
		SettingsDisableGroupHeaders,
		SettingsFindAsYouType,
		SettingsIncludeSetInTrackNumbers,
		SettingsShowATEInPlaylistView,
		SettingsCurrentMediaInTitle,
		SettingsShowFileMenuItemLibraryPane,
		SettingsShowFileMenuItemListPane,
		SettingsShowTagEditorPlusLibraryPane,
		SettingsShowTagEditorPlusListPane,
		SettingsShowTagEditorPlusForOtherTypes,
		SettingsRestoreLastPlaylist,
		SettingsRestoreLastMedia,
		SettingsRestoreLastPlayState,
		SettingsRestoreLastPosition,
		SettingsRestorePaused,
		SettingsPartyShuffleConfigured,
		SettingsPartyShufflePendingSourceChange,
		SettingsSpaceBarPlayPause,
		SettingsMSNATFString,
		SettingsTaskbarATFString,
		SettingsPartyShufflePlaylistName,
		SettingsPartyShufflePlaylistFilename,
		SettingsPartyShuffleSourcePlaylistName,
		SettingsPartyShuffleSourcePlaylistFilename,
		SettingsLastUnsupportedVersion,
		SettingsTagEditorWidth,
		SettingsTagEditorHeight,
		SettingsTagEditorPage,
		SettingsMessageBoxWidth,
		SettingsMessageBoxHeight,
		SettingsRestorePositionRewind,
		SettingsPartyShufflePlayedSongs,
		SettingsPartyShuffleUpcomingSongs,
		SettingsPartyShufflePlaylistIndex,
		SettingsIncludeSetMaxItemCount,
		SettingsPlayNextMaxItemCount,
	};

	public static class WMPPlusCore {
		public const string WMPPlusRegKey = @"Software\BM-productions\WMP Plus";

		public static WMPPlusHookData Hook { get; private set; }
		public static PluginManager Plugins { get; private set; }
		public static PluginRestore RestorePlugins { get; private set; }
		public static MediaHook Media { get; private set; }
		public static LibraryHook Library { get; private set; }
		public static HotkeysCore Hotkeys { get; private set; }

		[return: MarshalAs(UnmanagedType.SysInt)]
		[DllImport(WMPPlusPublic.WMPPlusHookName, EntryPoint = "#101")]
		internal static extern IntPtr GetPtrProp(WMPPlusProperty index);
		[DllImport(WMPPlusPublic.WMPPlusHookName, EntryPoint = "#102")]
		internal static extern void SetPtrProp(WMPPlusProperty index,
			[MarshalAs(UnmanagedType.SysInt)] IntPtr value);
		[return: MarshalAs(UnmanagedType.LPTStr)]
		[DllImport(WMPPlusPublic.WMPPlusHookName, EntryPoint = "#101")]
		internal static extern string GetStrProp(WMPPlusProperty index);
		[DllImport(WMPPlusPublic.WMPPlusHookName, EntryPoint = "#102")]
		internal static extern void SetStrProp(WMPPlusProperty index,
			[MarshalAs(UnmanagedType.LPTStr)] string value);
		internal static int GetIntProp(WMPPlusProperty index) {
			return GetPtrProp(index).ToInt32();
		}
		internal static void SetIntProp(WMPPlusProperty index, int value) {
			SetPtrProp(index, (IntPtr)value);
		}
		internal static bool GetBoolProp(WMPPlusProperty index) {
			return GetPtrProp(index) != IntPtr.Zero;
		}
		internal static void SetBoolProp(WMPPlusProperty index, bool value) {
			SetPtrProp(index, value ? (IntPtr)1 : (IntPtr)0);
		}

		static WMPPlusCore() {
			Hook = new WMPPlusHookData();
			Plugins = new PluginManager();
			RestorePlugins = new PluginRestore(Hook, Plugins);
			Media = new MediaHook();
			Library = new LibraryHook(Hook);
			Hotkeys = new HotkeysCore();
		}

		public static void Dispose() {
			Hook = null;
			Plugins.Free();
			Plugins = null;
			RestorePlugins.Free();
			RestorePlugins = null;
			Media = null;
			Library = null;
			Hotkeys = null;
		}
	}
}
